//
//  NSNetworkInterface.m
//  erhuo
//
//  Created by Jingru Nie on 15/5/12.
//  Copyright (c) 2015年 集易（北京）科技有限公司. All rights reserved.
//

#import "NSNetworkInterface.h"
#import "NSNetwork.h"

@interface NSNetworkInterface()
{
    id<NSNetworkInterfaceDelegate> _delegate;
    _ERHUO_NETWORK_INTERFACE_ _type;
    _NETWORK_DATA_TYPE_ _datatype;
    NSNetwork *_network;
}
@end

#define _ERHUO_INTERFACE_BASE_ @"https://www.erhuoapp.com/interface/"

@implementation NSNetworkInterface

- (_ERHUO_NETWORK_INTERFACE_)requestType {
    return _type;
}

- (instancetype)init {
    return [self initWith:_ERHUO_NETWORK_INTERFACE_NULL_ withData:nil];
}

- (instancetype)initWith:(_ERHUO_NETWORK_INTERFACE_)type withData:(NSDictionary *)data {
    self = [super init];
    
    _type = type;
    _datatype = _NETWORK_DATA_TYPE_TEXT_;
    NSDictionary *formdata = [self generatePostData:type withData:data];
    
    _network = [[NSNetwork alloc] initWithURL:[formdata objectForKey:@"url"] withMethod:_NETWORK_METHOD_POST_ withData:[formdata objectForKey:@"formdata"] withType:_datatype];
    
    if (_type == _ERHUO_NETWORK_INTERFACE_EXPRESS_QUERY_) {
        _network = [[NSNetwork alloc] initWithURL:[formdata objectForKey:@"url"] withMethod:_NETWORK_METHOD_GET_ withData:nil withType:_datatype];
    }
    
    
    return self;
}

- (void)sendRequest {
    if (_type == _ERHUO_NETWORK_INTERFACE_NULL_) {
        return;
    }
    
    [_network sendRequest:^(NSURLResponse *response, NSData *result, NSError *error) {
        NSString *errdescription;
        NSInteger code = 0;
        NSDictionary *data;
        if (!error) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableLeaves error:nil];
            
            code = [[dic objectForKey:@"error"] integerValue];
            data = [dic objectForKey:@"data"];
            
            if (_type == _ERHUO_NETWORK_INTERFACE_EXPRESS_QUERY_) {
                code = [[dic objectForKey:@"status"] integerValue];
                
                switch (code) {
                    case 0:
                        
                    case 2:
                    {
                        code = -3;
                        errdescription = @"暂无查询结果";
                    }
                        break;
                        
                    case 1:
                    {
                        code = 0;
                    }
                        break;
                        
                    default:
                        break;
                }
                data = dic;
            }
            
            switch (code) {
                case 0:{
                    if ([[dic objectForKey:@"description"] isEqualToString:@"cellphone_auth"]) {
                        code = -1;
                    }else if ([[dic objectForKey:@"description"] isEqualToString:@"idcard_auth"]) {
                        code = -2;
                    }else if ([[dic objectForKey:@"description"] isEqualToString:@"nickname_already_exist"]) {
                        code = 1001;
                        errdescription = @"昵称已存在";
                    }
                }
                    break;
                    
                case 1000:
                    errdescription = @"用户名/密码错误";
                    break;
                    
                case 1001:
                    errdescription = @"昵称已存在";
                    break;
                    
                case 1003:
                    errdescription = @"当前用户不存在";
                    break;
                    
                case 1004:
                    errdescription = @"该手机号已经被绑定";
                    break;
                    
                case 1005:
                    errdescription = @"该手机号未被绑定";
                    break;
                    
                case 1006:
                    errdescription = @"该手机号未验证";
                    break;
                    
                case 1007:
                    errdescription = @"密码未验证";
                    break;
                    
                case 1029:
                    errdescription = @"您的钱包余额发生变动，请重新提交";
                    break;
                    
                case 1034:
                    errdescription = @"商品已经有人下单啰";
                    break;
                    
                case 1039:
                    errdescription = @"不能购买自己的商品哟~";
                    break;
                    
                case 1040:
                    errdescription = @"您已经购买了当前商品，快去支付吧~";
                    break;
                    
                case 2000:
                    errdescription = @"请认证后再发布商品";
                    break;
                    
                case 2001:
                    errdescription = @"用户未登录";
                    break;
                    
                case 2002:
                    errdescription = @"尝试次数过多，请2小时候重新登录";
                    break;
                    
                case 2003:
                    errdescription = @"请求验证码";
                    break;
                    
                case 2005:
                    errdescription = @"提现金额过小";
                    break;
                    
                case 2009:
                    errdescription = @"您的未付款订单过多";
                    break;
                    
                default:{
                    if ([[dic objectForKey:@"verror"] integerValue]) {
                        errdescription = [dic objectForKey:@"description"];
                    }else {
                        errdescription = @"请求失败";
                    }
                }
                    break;
            }
        }else {
            code = 10000;
            errdescription = @"网络似乎有问题";
        }
        [_delegate networkInterface:self ResponseWith:data code:code error:errdescription];
    }];
}


/**
 *  根据接口类型生成 POST 请求所需数据
 *
 *  @param type 接口类型，由 _ERHUO_NETWORK_INTERFACE_ 定义
 *  @param data POST 数据
 *
 *  @return 接口请求所需的数据
 */
- (NSDictionary *)generatePostData:(_ERHUO_NETWORK_INTERFACE_)type withData:(NSDictionary *)data {
    NSMutableDictionary *formdata = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @"client", nil];
    NSURL *url;
    
    switch (type) {
        case _ERHUO_NETWORK_INTERFACE_USER_LOGIN_: {
            [formdata setObject:[data objectForKey:@"phone"] forKey:@"phone"];
            [formdata setObject:[data objectForKey:@"pwd"] forKey:@"pwd"];
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"user_login/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_POST_ADDRESS_LIST_: {
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"post_address_list/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_ORDER_INSERT_: {
            [formdata setObject:[data objectForKey:@"sid"] forKey:@"sid"];
            [formdata setObject:[data objectForKey:@"aid"] forKey:@"aid"];
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"goods_order/insert/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_POST_ADDRESS_INSERT_: {
            [formdata setObject:[data objectForKey:@"consignee"] forKey:@"consignee"];
            [formdata setObject:[data objectForKey:@"phone"] forKey:@"phone"];
            [formdata setObject:[data objectForKey:@"province"] forKey:@"province"];
            [formdata setObject:[data objectForKey:@"city"] forKey:@"city"];
            [formdata setObject:[data objectForKey:@"district"] forKey:@"district"];
            [formdata setObject:[data objectForKey:@"detail"] forKey:@"detail"];
            [formdata setObject:[data objectForKey:@"postcode"] forKey:@"postcode"];
            [formdata setObject:[data objectForKey:@"selected"] forKey:@"selected"];
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"post_address_insert/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_POST_ADDRESS_UPDATE_: {
            [formdata setObject:[data objectForKey:@"consignee"] forKey:@"consignee"];
            [formdata setObject:[data objectForKey:@"phone"] forKey:@"phone"];
            [formdata setObject:[data objectForKey:@"province"] forKey:@"province"];
            [formdata setObject:[data objectForKey:@"city"] forKey:@"city"];
            [formdata setObject:[data objectForKey:@"district"] forKey:@"district"];
            [formdata setObject:[data objectForKey:@"detail"] forKey:@"detail"];
            [formdata setObject:[data objectForKey:@"postcode"] forKey:@"postcode"];
            [formdata setObject:[data objectForKey:@"selected"] forKey:@"selected"];
            [formdata setObject:[data objectForKey:@"aid"] forKey:@"aid"];
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"post_address_update/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_ORDER_LIST_SELL_: {
            [formdata setObject:[data objectForKey:@"index"] forKey:@"index"];
            [formdata setObject:[data objectForKey:@"number"] forKey:@"number"];
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"goods_list/sell/user/sold/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_ORDER_LIST_BUY_: {
            [formdata setObject:[data objectForKey:@"index"] forKey:@"index"];
            [formdata setObject:[data objectForKey:@"number"] forKey:@"number"];
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"goods_list/sell/user/buy/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_ORDER_DETAIL_: {
            [formdata setObject:[data objectForKey:@"oid"] forKey:@"oid"];
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"goods_order/detail/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_ORDER_UPDATE_: {
            [formdata setObject:[data objectForKey:@"oid"] forKey:@"oid"];
            [formdata setObject:[data objectForKey:@"price"] forKey:@"price"];
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"goods_order/update/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_DRAWBACK_CANCEL_: {
            [formdata setObject:[data objectForKey:@"did"] forKey:@"did"];
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"goods_order/drawback/delete/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_DRAWBACK_DEAL_: {
            [formdata setObject:[data objectForKey:@"did"] forKey:@"did"];
            [formdata setObject:[data objectForKey:@"deal"] forKey:@"deal"];
            if ([[data objectForKey:@"deal"] integerValue]) {
                //同意
                [formdata setObject:[data objectForKey:@"pwd"] forKey:@"pwd"];
            }else {
                //拒绝
                [formdata setObject:[data objectForKey:@"reason"] forKey:@"reason"];
            }
            
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"goods_order/drawback/deal/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_DRAWBACK_APPLY_: {
            [formdata setObject:[data objectForKey:@"oid"] forKey:@"oid"];
            [formdata setObject:[data objectForKey:@"reason"] forKey:@"reason"];
            [formdata setObject:[data objectForKey:@"price"] forKey:@"price"];
            
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"goods_order/drawback/apply/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_ORDER_CLOSE_: {
            [formdata setObject:[data objectForKey:@"oid"] forKey:@"oid"];
            
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"goods_order/close/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_ORDER_REMIND_: {
            [formdata setObject:[data objectForKey:@"oid"] forKey:@"oid"];
            
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"goods_order/remind/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_ORDER_SEND_: {
            [formdata setObject:[data objectForKey:@"oid"] forKey:@"oid"];
            [formdata setObject:[data objectForKey:@"method"] forKey:@"method"];
            if ([[data objectForKey:@"method"] integerValue] == 2) {
                [formdata setObject:[data objectForKey:@"express"] forKey:@"express"];
                [formdata setObject:[data objectForKey:@"expressid"] forKey:@"expressid"];
            }
            
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"goods_order/send/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_ORDER_WALLET_: {
            [formdata setObject:[data objectForKey:@"oid"] forKey:@"oid"];
            
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"goods_order/wallet/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_ORDER_WILLPAY_: {
            [formdata setObject:[data objectForKey:@"oid"] forKey:@"oid"];
            [formdata setObject:[data objectForKey:@"thirdpay"] forKey:@"thirdpay"];
            
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"goods_order/willpay/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_ORDER_PAY_: {
            [formdata setObject:[data objectForKey:@"alipaystring"] forKey:@"alipaystring"];
            
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"goods_order/pay/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_ORDER_RECEIVE_: {
            [formdata setObject:[data objectForKey:@"oid"] forKey:@"oid"];
            [formdata setObject:[data objectForKey:@"pwd"] forKey:@"pwd"];
            
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"goods_order/receive/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_EXPRESS_QUERY_: {
            NSString *urlstring = [NSString stringWithFormat:@"http://api.kuaidi100.com/api?id=024639e97989511b&com=%@&nu=%@&show=0&muti=1&order=desc", [data objectForKey:@"express"], [data objectForKey:@"expressid"]];
            urlstring = [urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            url = [NSURL URLWithString:urlstring];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_BILLS_QUERY_: {
            if ([[data allKeys] containsObject:@"index"]) {
                [formdata setObject:[data objectForKey:@"index"] forKey:@"index"];
            }
            if ([[data allKeys] containsObject:@"number"]) {
                [formdata setObject:[data objectForKey:@"number"] forKey:@"number"];
            }
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"user_bills/"]];
        }
            break;
            
        case _ERHUO_NETWORK_INTERFACE_WITHDRAW_APPLY_: {
            [formdata setObject:[data objectForKey:@"payment"] forKey:@"payment"];
            [formdata setObject:[data objectForKey:@"pwd"] forKey:@"pwd"];
            [formdata setObject:[data objectForKey:@"payid"] forKey:@"payid"];
            [formdata setObject:[data objectForKey:@"realname"] forKey:@"realname"];
            [formdata setObject:[data objectForKey:@"price"] forKey:@"price"];
            
            url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", _ERHUO_INTERFACE_BASE_, @"user_money_withdraw/"]];
        }
            break;
            
        default:
            break;
    }
    return [NSDictionary dictionaryWithObjectsAndKeys:url, @"url", formdata, @"formdata", nil];
}



@end
